<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<c:import url="/includes/header.jsp"></c:import>
</head>
<body>
	<div class="container">
		<form id="listForm" method="post"
			action="${pageContext.request.contextPath}/admin/resource/index.do">
			<div class="row">
				<div class="col-md-4" style="padding: 0; margin-bottom: 5px;">
					<a class="btn btn-primary"
						href="${pageContext.request.contextPath}/admin/resource/add.do">+
						新增</a>
				</div>
				<div class="col-md-4"></div>
			</div>
			<div class="row">
				<table class="tab table table-striped table-bordered">
					<tr>
						<th>ID</th>
						<th>资源名</th>
						<th>资源地址</th>
						<th>PID</th>
						<th>操作</th>
					</tr>
					<c:forEach items="${list}" var="res">
						<tr>
							<td>${res.id}</td>
							<td>${res.name}</td>
							<td>${res.url}</td>
							<td>${res.parentId}</td>
							<td><a class=" edit btn btn-sm btn-default" data-id="${res.id}">编辑</a>&nbsp;&nbsp;&nbsp;
								<a class="btn btn-sm delete btn-default" data-id="${res.id}">删除</a>
							</td>
						</tr>
					</c:forEach>
				</table>
				<c:import url="/includes/page.jsp"></c:import>
			</div>
		</form>
	</div>
</body>
</html>

<script>
	$(function() {
		$(".delete")
				.click(
						function() {
							var data = {};
							var id = $(this).attr("data-id");
							data.resourceId = id;
							data.url = "${pageContext.request.contextPath}/admin/resource/delete.do";
							bootbox.confirm("您确定要删除该资源吗？", function(result) {
								if (result) {
									sendRequest(data);
								}
							});
						});

		$(".edit")
				.click(
						function() {
							var id = $(this).attr("data-id");
							var url = "${pageContext.request.contextPath}/admin/resource/edit.do?resourceId="
									+ id;
							window.location.href = url;
						});

		var sendRequest = function(data) {
			$.ajax({
				url : data.url,
				type : "post",
				dataType : "json",
				data : $.param(data),
				async : true,
				success : function(data) {
					if (data.code == 0) {
						bootbox.alert(data.data);
						bootbox.confirm(data.data, function(result) {
							window.location.reload();
						});
					} else {
						bootbox.alert(data.msg);
					}
				},
				error : function() {
					bootbox.alert("网络繁忙,请求失败");
				}
			});
		}
	})
</script>